<template>
  <NModal :show="state.show" @UpdateShow="close">
    <div ref="modalRef" class="list-dia">
      <MacHeader
        :title="state.title"
        :isFullscreen="isFullscreen"
        :showDesc="false"
        @toggle="toggle"
        @close="close"
      />
      <div class="content">
        <div>
          <NForm inline :model="tableInfo.name" label-placement="left">
            <NFormItem label="关联资源">
              <NInput
                v-model:value="tableInfo.queryForm.query"
                placeholder="请输入资源标题"
                clearable
                style="width: 200px"
              />
            </NFormItem>
            <n-form-item label="分类">
              <n-select
                v-model:value="tableInfo.queryForm.title"
                :options="reportTitleEnum"
                label-field="label"
                placeholder="请选择分类"
                clearable
                value-field="value"
                style="width: 200px"
              />
            </n-form-item>
            <n-form-item label="状态">
              <n-select
                v-model:value="tableInfo.queryForm.status"
                :options="reportStatusEnum"
                label-field="label"
                placeholder="请选择状态"
                clearable
                value-field="value"
                style="width: 200px"
              />
            </n-form-item>
            <NFormItem>
              <NButton type="primary" @click="queryData">
                <template #icon>
                  <n-icon>
                    <i class="esayIcon esayIcon-sousuo"></i>
                  </n-icon>
                </template>
                <span>搜索</span>
              </NButton>
            </NFormItem>
          </NForm>
        </div>
        <NDataTable
          style="flex: 1"
          flex-height
          :loading="tableInfo.listLoading"
          :columns="columns"
          :data="tableInfo.tableData"
          :pagination="false"
        />
        <div class="pagination">
          <NPagination
            v-model:page="tableInfo.queryForm.pageNo"
            :page-count="tableInfo.total"
            :page-sizes="tableInfo.pageSizes"
            show-quick-jumper
            show-size-picker
            @updatePage="pageChange"
            @updatePageSize="sizeChange"
          />
        </div>
      </div>
    </div>
  </NModal>
</template>
<script setup lang="ts">
import { ref, reactive, h } from 'vue'
import type { Ref } from 'vue'
import {
  DataTableColumns,
  NModal,
  NDataTable,
  NPagination,
  NInput,
  NForm,
  NFormItem,
  NButton,
  NIcon,
  NSelect,
  NSwitch
} from 'naive-ui'
import MacHeader from '@/components/codeBlock/MacHeader'
import { useFullscreen } from '@vueuse/core'
import { useApi } from '@/hooks/useApi'
import reportTitleEnum from '@/enum/reportTitleEnum'
import reportStatusEnum from '@/enum/reportStatusEnum'
import reportApi from '@/service/api/photo/report'
const handDetail = (row: any) => {
  console.log(row)
}
const addRef: Ref<any> = ref()
const delRef: Ref<any> = ref()
const { tableInfo, pageChange, sizeChange, queryData } = useApi({
  isRequest: false,
  addRef,
  delRef,
  getData: reportApi.list,
  dataMap: (data: any) => {
    return data.map((el: any) => {
      el.loading = false
      return el
    })
  },
  initForm: {
    user_id: null,
    title: null,
    status: null
  }
})

const columns: DataTableColumns<any> = [
  {
    title: '资源标题',
    key: 'resoucename',
    align: 'center',
    render(row: any) {
      return h(
        'div',
        {
          class: 'cursor-pointer',
          onClick: () => handDetail(row)
        },
        { default: () => row.photo_name }
      )
    }
  },
  {
    title: '分类',
    key: 'title'
  },
  {
    title: '描述',
    key: 'content',
    width: 300,
    ellipsis: true
  },
  {
    title: '举报日期',
    key: 'create_at'
  },
  {
    title: '是否处理',
    key: 'status',
    width: 100,
    align: 'center',
    render(row: any) {
      return h(NSwitch, {
        value: row.status,
        loading: row.loading,
        rubberBand: false,
        checkedValue: 1,
        uncheckedValue: 0,
        ['on-update:value']: (checked: string) => {
          row.loading = true
          reportApi
            .updStatus({
              id: row.id,
              status: checked
            })
            .then(({ code }) => {
              if (code === 200) {
                row.status = checked
              }
              row.loading = false
            })
        }
      })
    }
  }
]

const state = reactive({
  show: false,
  title: ''
})
const modalRef: Ref<any> = ref()
const {
  isFullscreen,
  toggle
}: { isFullscreen: Ref<boolean>; toggle: () => Promise<void> } =
  useFullscreen(modalRef)
const close = () => {
  state.show = false
}
const open = (row: any): void => {
  state.title = `${row.nickname}[购买列表]`
  tableInfo.queryForm.userid = row.id
  queryData()
  state.show = true
}

defineExpose({
  open
})
</script>
<style scoped lang="scss">
.list-dia {
  width: 850px;
  height: 80vh;
  background: #fff;
  padding: 10px;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  .content {
    margin-top: 20px;
    flex: 1;
    display: flex;
    flex-direction: column;
  }
  .add-text {
    margin-left: 10px;
  }
  .pb20 {
    padding-bottom: 20px;
  }
  .pagination {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
